import random

from django.http import JsonResponse
from django.shortcuts import render, get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.views import login_required

from .forms import CommentForm
from .models import Ads, Book, Chapter, Category, Channel, WordCount
from django.shortcuts import redirect
from django.urls import reverse


# Create your views here.
def homepage(request):
    books = Book.objects.all()
    rbs = random.sample(list(books), k=7)
    adss = Ads.objects.all()
    rbs2 = random.sample(list(books), k=4)
    return render(request, "main/homepage.html", context={'books': books, 'rbs': rbs, 'adss': adss, 'rbs2': rbs2})


def bookstore(request):
    channel_books = Channel.objects.all()
    books = Book.objects.all()
    category_books = Category.objects.all()
    wordcount_books = WordCount.objects.all()

    return render(request, 'main/bookstore.html',
                  {'books': books, 'channel_books': channel_books, 'category_books': category_books,
                   'wordcount_books': wordcount_books})


def bookstoredetailchannel(request, channel_id):
    channel = Channel.objects.get(id=channel_id)
    if channel:
        books = Book.objects.filter(channel=channel)
        return render(request, 'main/bookstore-detail-channel.html', context={'books': books})


def bookstoredetailcategory(request, category_id):
    category = Category.objects.get(id=category_id)
    if category:
        books = Book.objects.filter(category=category)
        return render(request, 'main/bookstore-detail-category.html', context={'books': books})


def bookstoredetailwordcount(request, wordcount_id):
    wordcount = WordCount.objects.get(id=wordcount_id)
    if wordcount:
        books = Book.objects.filter(word_count=wordcount)
        return render(request, 'main/bookstore-detail-wordcount.html', context={'books': books})


def ranking(request):
    books = Book.objects.order_by('-click')
    return render(request, 'main/ranking.html', context={'books': books})


def detail(request, num):
    books = Book.objects.get(id=num)
    chapter = books.chapter.all()
    return render(request, 'main/detail.html', context={'books': books, 'chapter': chapter})


@login_required(redirect_field_name='next', login_url='/user/login/')
def chapter_detail(request, book_id, chapter_id):
    chapter = get_object_or_404(Chapter, id=chapter_id)
    books = get_object_or_404(Book, id=book_id)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.chapter = chapter
            comment.user = request.user
            comment.save()
            return redirect(request.META.get('HTTP_REFERER', '/'), novel_id=books.id, chapter_id=chapter.id)
    else:
        form = CommentForm()
    chapters = list(books.chapter.all())
    current_index = chapters.index(chapter)
    previous_chapter = chapters[current_index - 1] if current_index > 0 else None
    next_chapter = chapters[current_index + 1] if current_index < len(chapters) - 1 else None
    context = {
        'chapter': chapter,
        'previous_chapter': previous_chapter,
        'next_chapter': next_chapter,
        'books': books,
        'form': form,
        'comments': chapter.comments.all(),
    }

    return render(request, 'main/chapter_detail.html', context)
